let user = JSON.parse(localStorage.getItem("user"));
let rootWrapper = document.getElementById("root-wrapper");
let rootLoading = document.getElementById("root-loading");

window.addEventListener("load", winLoad);

function winLoad()
{
    if (user)
        winLoadChat();
    else
        winLoadLogin();
}

function winLoadChat()
{
    rootWrapper.style.opacity = 0;
    rootLoading.style.opacity = 1;
    fetch("/ai/html/chat.html", { method: "GET" })
       .then(response => 
            {
                if (response.ok)
                    return response.text();
                else
                    throw new Error("Network response was not ok");
            })
       .then(data => 
            {
                rootWrapper.innerHTML = data;
                fetch("/ai/js/chat.js", { method: "GET" })
                   .then(response => 
                    {
                        if (response.ok)
                            return response.text();
                        else
                            throw new Error("Network response was not ok");
                    })
                   .then(data => eval(data))
                   .catch(error => console.error("winLoadLogin.js error:", error));
            })
       .catch(error => console.error("winLoadChat error:", error));
}

function winLoadLogin()
{
    fetch("/ai/html/login.html", { method: "GET" })
       .then(response => 
            {
                if (response.ok)
                    return response.text();
                else
                    throw new Error("Network response was not ok");
            })
       .then(data => 
            {
                rootWrapper.innerHTML = data;
                fetch("/ai/js/login.js", { method: "GET" })
                   .then(response => 
                    {
                        if (response.ok)
                            return response.text();
                        else
                            throw new Error("Network response was not ok");
                    })
                   .then(data => eval(data))
                   .catch(error => console.error("winLoadLogin.js error:", error));
            })
       .catch(error => console.error("winLoadLogin error:", error));
}